package com.triangle.cashier.widget;


import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.triangle.cashier.R;

/**
 * 选择会员
 * Created by  on 2018/6/30.
 */

public class SelectMemberDialog extends DialogFragment implements View.OnClickListener {

    private OnChangeSelectedClickListener onChangeSelectedClickListener;

    private EditText memberNumEditText;
    private Activity activity;
    private StringBuffer sb;
    private TextView nameTextView;//名称
    private TextView balanceTextView;//余额
    private TextView levelTextView;//等级
    private TextView saleTextView;//折扣
    private TextView integralTextView;//积分
    private TextView phoneTextView;//电话


    public SelectMemberDialog(Activity activity, OnChangeSelectedClickListener onChangeSelectedClickListener) {
        this.onChangeSelectedClickListener = onChangeSelectedClickListener;
        this.activity = activity;
    }

    public SelectMemberDialog() {
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View customView = LayoutInflater.from(getActivity()).inflate(
                R.layout.select_member, null);
        initView(customView);
        Dialog dialog = new Dialog(getActivity(), R.style.soft_para_dialog);
        dialog.setContentView(customView);
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
//        window.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_windows_bg));
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = 1200;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        return dialog;
    }

    private void initView(View view) {
        ImageButton closeBtn = view.findViewById(R.id.close_btn);
        memberNumEditText = view.findViewById(R.id.member_num_editText);
        nameTextView = view.findViewById(R.id.name_TV);
        balanceTextView = view.findViewById(R.id.balance_TV);
        levelTextView = view.findViewById(R.id.level_TV);
        saleTextView = view.findViewById(R.id.sale_TV);
        integralTextView = view.findViewById(R.id.integral_TV);
        phoneTextView = view.findViewById(R.id.phone_TV);
        memberNumEditText.setFocusable(false);//不让该edittext获得焦点
        memberNumEditText.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                memberNumEditText.setInputType(InputType.TYPE_NULL); // 关闭软键盘，这样当点击该edittext的时候，不会弹出系统自带的输入法
                return false;
            }
        });
        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onChangeSelectedClickListener.cancel();
            }
        });
        ImageButton cancelBtn = view.findViewById(R.id.cancel_button);
        ImageButton okBtn = view.findViewById(R.id.ok_button);
        Button key0 = view.findViewById(R.id.key_0);
        Button key1 = view.findViewById(R.id.key_1);
        Button key2 = view.findViewById(R.id.key_2);
        Button key3 = view.findViewById(R.id.key_3);
        Button key4 = view.findViewById(R.id.key_4);
        Button key5 = view.findViewById(R.id.key_5);
        Button key6 = view.findViewById(R.id.key_6);
        Button key7 = view.findViewById(R.id.key_7);
        Button key8 = view.findViewById(R.id.key_8);
        Button key9 = view.findViewById(R.id.key_9);
        ImageButton keyDelete = view.findViewById(R.id.key_delete);
        Button keyOk = view.findViewById(R.id.key_ok);
        cancelBtn.setOnClickListener(this);
        okBtn.setOnClickListener(this);
        key0.setOnClickListener(this);
        key1.setOnClickListener(this);
        key2.setOnClickListener(this);
        key3.setOnClickListener(this);
        key4.setOnClickListener(this);
        key5.setOnClickListener(this);
        key6.setOnClickListener(this);
        key7.setOnClickListener(this);
        key8.setOnClickListener(this);
        key9.setOnClickListener(this);
        keyDelete.setOnClickListener(this);
        keyOk.setOnClickListener(this);

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onStart() {

        super.onStart();
        Window window = this.getDialog().getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        window.getDecorView().setSystemUiVisibility(uiOptions);

    }

    @Override
    public void onClick(View v) {
        String text = memberNumEditText.getText().toString();
        sb = new StringBuffer(text);
        switch (v.getId()) {
            case R.id.cancel_button:
                onChangeSelectedClickListener.cancel();
                break;
            case R.id.ok_button:
                onChangeSelectedClickListener.ok();
                break;
            case R.id.key_0:
                sb.append("0");
                break;
            case R.id.key_1:
                sb.append("1");
                break;
            case R.id.key_2:
                sb.append("2");
                break;
            case R.id.key_3:
                sb.append("3");
                break;
            case R.id.key_4:
                sb.append("4");
                break;
            case R.id.key_5:
                sb.append("5");
                break;
            case R.id.key_6:
                sb.append("6");
                break;
            case R.id.key_7:
                sb.append("7");
                break;
            case R.id.key_8:
                sb.append("8");
                break;
            case R.id.key_9:
                sb.append("9");
                break;
            case R.id.key_delete:
                sb.setLength(0);
                break;
            case R.id.key_ok:

                break;

        }
        memberNumEditText.setText(sb.toString());
    }

    public interface OnChangeSelectedClickListener {

        void cancel();

        void ok();

    }

}
